package cn.tedu._03vehicle.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("v1/user/")
@Tag(name = "用户管理模块")
public class UserController {

    @Autowired
    UserMapper userMapper;
    @PostMapping("reg")
    @Operation(summary = "01-用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam) {
        log.debug("开始处理【用户注册】的请求，参数：{}", userRegParam);
        int rows = userMapper.selectByUsername(userRegParam.getUsername());
        if (rows > 0) {
            return new  JsonResult(StatusCode.USERNAME_EXISTS);
        }else {
            User user = new User();
            BeanUtils.copyProperties(userRegParam, user);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            int r = userMapper.insertUser(user);
            if ( r> 0){
                return JsonResult.ok();
            }else {
                return new JsonResult(StatusCode.UKNOWN_ERROR);
            }
        }
    }
    @PostMapping("login")
    @Operation(summary = "02-用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession  session) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginParam);
        //判断用户名密码正确否
        UserVO userVO = userMapper.SelectByUsernameAndPassword(userLoginParam.getUsername(), userLoginParam.getPassword());
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //在服务器端保存用户的会话标识
        session.setAttribute("user", userVO);

        return JsonResult.ok(userVO);
    }

    //获取当前登录用户信息
    @GetMapping("currentUser")
    @Operation(summary = "03-获取当前登录用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession  session) {
        log.debug("开始处理【获取当前登录用户信息】的请求");
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }
    @PostMapping("logout")
    @Operation(summary = "04-用户退出")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession  session) {
        log.debug("开始处理【用户注销】的请求");
        session.removeAttribute("user");
    }
}
